package com.code2roc.jarmanage.filter;

import com.code2roc.jarmanage.util.CommonUtil;
import com.code2roc.jarmanage.util.ConfigUtil;
import com.code2roc.jarmanage.util.ConvertUtil;
import com.code2roc.jarmanage.util.JedisUtil;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RedisOpenCheckFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        boolean open = ConvertUtil.convert2Boolean(ConfigUtil.getConfigValue("redis","open"));
        if (open) {
            if(!JedisUtil.checkConnect()){
                response.sendRedirect(CommonUtil.getRooptPath(request) + "pages/error/603.html");
            }else{
                filterChain.doFilter(servletRequest, servletResponse);
            }
        } else {
            response.sendRedirect(CommonUtil.getRooptPath(request) + "pages/error/602.html");
        }
    }
}
